package com.heima.wemedia.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Wrapper;
import java.util.Date;

@RestController
@RequestMapping("/api/v1/user")
public class WmUserController {

    @Autowired
    WmUserService service;

    /**
     * 判断当前自媒体用户是否被注册,返回数据 != 0 则被注册
     * @param name
     * @return
     */
    @GetMapping("/findByName/{name}")
    public int findByName(@PathVariable  String name) {
        LambdaQueryWrapper<WmUser> wrapper = Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name);
        return service.count(wrapper);
    }


    /**
     * 保存一条自媒体用户信息
     * 数据为1则保存成功
     * @param wmUser
     * @return
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody WmUser wmUser){
        if (wmUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"数据不存在");
        }else{
            wmUser.setCreatedTime(new Date());
            service.save(wmUser);
            return ResponseResult.okResult(1);
        }
    }
}
